// ==UserScript==
// @name 东软日报
// @namespace http://git.oschina.net/baOZe/userscript
// @updateURL http://git.oschina.net/baOZe/userscript/raw/master/%E4%B8%9C%E8%BD%AF%E6%97%A5%E6%8A%A5.user.js?dir=0&filepath=%E4%B8%9C%E8%BD%AF%E6%97%A5%E6%8A%A5.user.js
// @version 0.4
// @description 点击增加按钮后，会自动填入必填的所有数据。已经可以增加多个，并自动递增时间，再一并提交。
// @author tong.zh
// @match http://192.168.131.211:9997/projectmis/daily.do?resourceId=project:rbtx
// @grant none
// @run-at document-end
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    //dojo.disconnect(unieap.byId('btnAdd'));

    var data;
    
    function buildData(){
        data = {
            XM_ID:getValueByName("dailyXM","101JYAD11016_公共研发与创新中心共通项目"),//'BB480000000000000762',
            //DAY:1474819200000,
            query_taskno:'',
            DAILY_RWMC:'',
            //WCBJ:'2',
            //KS_SJ:1472659200000,
            //JS_SJ:1472699200000,
            ZDSJ:'1',
            //GZL:'0',
            SFXC:getValueByName("GGSF","否"),//'0',
            //GZDD:'沈阳',
            HDDL:getValueByName("HDDL","实现"),//'5',
            HDLX:getValueByName("HDLX","编码"),//'5.1',
            MK:'',
            LCB:getValueByName("GSLCB","技术支持工作"),//'451014',
            //SPBZ:'0',
            HDMC:'新技术研发及技术支持',
            CGW:'',
            BZ:''
        };
    }

    function getValueByName(ds,name){
        var ds = dataCenter.getDataStore(ds);
        var value;
        ds.getRowSet().every(function(row,i){
            if(name == row.getItemValue("CODENAME")){
                value = row.getItemValue("CODEVALUE");
                return false;
            }
            return true;
        });
        return value;
    }
    
    function dailyadd() {
        //unieap.debug(dataCenter);
        
        //var store = unieap.byId('dailyForm').getBinding().getDataStore();
        var row = unieap.byId('dailyForm').getBinding().getRow();
        //var row = store.getRowSet().getRow(0);
        var day = row.getItemValue("DAY");
        var et = 3600000 * 8.5;
        var st = 3600000 * 17.5;
        row.setItemValue("KS_SJ",day + et);
        row.setItemValue("JS_SJ",day + st);
        
        row.setItemValue("DAY",getLatestStartDate());

        dojo.forEach(unieap.byId('dailyForm').getDescendants(), function(widget) {
            var v = data[widget.id];
            if(v){
                widget.setValue(v);
            }/*else{
                data[widget.id] = widget.getValue();
            }*/
        });
        computeTime();

    }
    
    function getLatestStartDate(){
        var rbsj_ks = unieap.byId('daily_rbsj_ks').getValue();		
		//var rbsj_js = unieap.byId('daily_rbsj_js').getValue();
        var count = unieap.byId('dailyGird').getRowManager().getRowCount();//contains the new record
        //var count = store.getRealRecordCount();
        //debugger;
        if(0 != count){
            rbsj_ks += 3600000 * 24 * (count - 1);
        }
        return rbsj_ks;
    }
    dojo.ready(function(){
        buildData();
        dojo.connect(unieap.byId('btnAdd'),'onClick',dailyadd);
    });
})();